package org.point.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.point.common.controller.BaseController;
import org.point.common.util.DoPage;
import org.point.domain.ConvertIndent;
import org.point.service.ConvertIndentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @ClassName: ConvertRuleDetailController  
 * @Description: TODO(这里用一句话描述这个类的作用)  
 * @author Kenny_Z' szqiuk@gmail.com
 * @date 2016年10月23日 下午9:17:53  
 *
 */
@Controller
@RequestMapping("/ConvertIn/")
public class ConvertIndentController extends BaseController {
	
	@Autowired
	private ConvertIndentService service;
	
	@RequestMapping(value = "ConvertIndentOrder.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void ConvertIndentList(@RequestParam(value = "memberUnit") String memberUnit,
			@RequestParam(value = "currentPage", required=false) Integer currentPage,
			@RequestParam(value = "keyWord", required=false) String keyWord,
			HttpServletResponse response) {

		JSONObject jsonObj = new JSONObject();
		DoPage<ConvertIndent> page = new DoPage<ConvertIndent>();
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (memberUnit != null) {
				map.put("memberUnit", memberUnit);
				page.setPageSize(10);
				if(currentPage != null){
					page.setCurrentPage(currentPage);
				}
				map.put("firstRecord", page.getFirstRecord());
				map.put("pageSize", page.getPageSize());
				
				if(strOperateUtil.isNullForString(keyWord)){
					map.put("keyWord", keyWord);
				}
				
				page.setMap(map);
				// 查询兑换规则分页数据
				DoPage<ConvertIndent> resultPage = service.searchProOrderForPage(page);
				// 返回分页参数，及兑换规则分类数据
				jsonObj.put("page", resultPage);
				// 查询兑换规则成功
				jsonObj.put("resultMsg", 1);
			} else {
				// 会员单位ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", 0);
			throw new RuntimeException("查询兑换规则分类错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (IOException e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
	}
	
	@RequestMapping(value = "insertPRORDER.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void insertCT(@ModelAttribute("ConvertIndent") ConvertIndent record, HttpServletResponse response){
		
		JSONObject jsonObj = new JSONObject();
		Integer result = 0;
		String ciCode = getNextNo("CI");
		try {
			if(record != null){
				record.setCreateDt(new Date());
				record.setCreateOper("Admin");
				record.setDelFlag(new Byte("0"));
				record.setMemberUnit("member001");
				result = service.insertProOrder(record,ciCode);
				// 兑换规则分类新增成功
				jsonObj.put("resultMsg", result);
			} else {
				// 兑换规则分类参数为null， 请检查参数！
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("兑换规则新增错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
	}
	
	@RequestMapping(value = "getProOrder.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void getProduct(@RequestParam(value = "id")String id, HttpServletResponse response){
		JSONObject jsonObj = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		String memberid = "member001";
		Integer result = 0;
		try {
			if(strOperateUtil.isNullForString(id)){
				map.put("id", Integer.parseInt(id));
				List<Hashtable<Object,Object>> products = service.getProducts(); 
				List<Hashtable<Object,Object>> users = service.getUsers(memberid);
				Hashtable<Object,Object> record = service.getProOrderByID(map);
				String user_id = record.get("user_id").toString();
				record.put("CIuser_id", user_id);
				jsonObj.put("record", record);
				jsonObj.put("products", products);
				jsonObj.put("users", users);
				// 查询兑换规则详细信息，成功
				jsonObj.put("resultMsg", 1);
			}else{
				// 兑换规则分类ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("查询兑换规则分类详细信息！错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
		
	}
	
	@RequestMapping(value = "openWindows.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void openWindows( HttpServletResponse response){
		JSONObject jsonObj = new JSONObject();
		String memberid = "member001";
		Integer result = 0;
		try {
			List<Hashtable<Object,Object>> products = service.getProducts(); 
			List<Hashtable<Object,Object>> users = service.getUsers(memberid);
			if(users!=null){
				jsonObj.put("users", users);
			}
			if(products!=null){
				jsonObj.put("products", products);
				// 查询兑换规则详细信息，成功
				jsonObj.put("resultMsg", 1);
			}else{
				// 兑换规则分类ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("查询兑换规则分类详细信息！错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
		
	}
	
	/**
	 * 
	 * @Title: delCT 
	 * @Description: TODO(批量删除兑换规则分类) 
	 * @param @param record    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	@RequestMapping(value = "delProOrder.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void delPRO(@RequestParam(value = "ids") String ids, HttpServletResponse response){
		// 测试
		JSONObject jsonObj = new JSONObject();
		Integer result = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		String newIDs = ids.substring(9, ids.length()-1);
		try {
			map.put("newIDs", newIDs);
			result = service.deleteProOrders(map);
			//result = service.deleteCT();
			if(result>0){
				// 兑换规则分类新增成功
				jsonObj.put("resultMsg", 1);
			} else {
				// 兑换规则分类参数为null， 请检查参数！
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("兑换规则新增错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
		
	}
	
	
	@RequestMapping(value = "verify.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void verify(@RequestParam(value = "ids") String ids, HttpServletResponse response){
		// 测试
		JSONObject jsonObj = new JSONObject();
		Integer result = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		String newIDs = ids.substring(9, ids.length()-1);
		try {
			map.put("newIDs", newIDs);
			result = service.verifyOrder(map);
			//result = service.deleteCT();
			if(result>0){
				// 兑换规则分类新增成功
				jsonObj.put("resultMsg", 1);
			} else {
				// 兑换规则分类参数为null， 请检查参数！
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("兑换规则新增错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
		
	}
	
	@RequestMapping(value="getDevInfo.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void getDevInfo(@RequestParam(value = "userid") String userid, HttpServletResponse response){
		// 测试
		JSONObject jsonObj = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map.put("userid", userid);
			Hashtable<Object,Object> devInfo = service.getDevInfo(map);
			//result = service.deleteCT();
			jsonObj.put("devInfo", devInfo);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("兑换规则新增错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
	}
	
	@RequestMapping(value="cancelOrder", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void cancelOrder(@RequestParam(value = "reason")String reason,@RequestParam(value = "id")String id, HttpServletResponse response){
		JSONObject jsonObj = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = 0;
		map.put("remarks", reason);
		map.put("id", id);
		try {
			String covid = service.getCovertId(map);
			if(covid!=null){
				map.put("order_id", covid);
				result = service.cancelOrder(map);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
		}finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
	}
	
}
